package com.xiashitech.agent.instrumentation.base.context.bridge;

import com.xiashitech.agent.instrumentation.base.context.*;
import io.opentelemetry.javaagent.shaded.io.opentelemetry.api.baggage.BaggageEntry;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.util.Map;
import java.util.function.BiConsumer;

@Immutable
public interface BridgeBaggage extends XSImplicitContextKeyed {

    static BridgeBaggage empty() {
        return BridgeImmutableBaggage.empty();
    }

    static BridgeBaggageBuilder builder() {
        return BridgeImmutableBaggage.builder();
    }

    static BridgeBaggage current() {
        return fromContext(XSContext.current());
    }

    static BridgeBaggage fromContext(XSContext context) {
        BridgeBaggage baggage = context.get(BridgeBaggageContextKey.KEY);
        return baggage != null ? baggage : empty();
    }

    @Nullable
    static BridgeBaggage fromContextOrNull(XSContext context) {
        return context.get(BridgeBaggageContextKey.KEY);
    }

    default XSContext storeInContext(XSContext context) {
        return context.with(BridgeBaggageContextKey.KEY, this);
    }

    int size();

    default boolean isEmpty() {
        return this.size() == 0;
    }

    void forEach(BiConsumer<? super String, ? super BaggageEntry> consumer);

    Map<String, BaggageEntry> asMap();

    @Nullable
    String getEntryValue(String key);

    BridgeBaggageBuilder toBuilder();
}
